<?php
include($_SERVER['DOCUMENT_ROOT'] . "/ObstacleWebApp/view/headeradmin.php");
?>

<section>

    <script>
        $(function() {
            $("#search").autocomplete({
                source: "zoekgebouw.php",
                minLength: 2
            });
        });
    </script>
    <hgroup>
        <h1>Gebouwen beheren</h1>
    </hgroup>
    <br>
    <form  name="searchform" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        <?php
        if (isset($_GET['search'])) { // If a search has been given, $_GET['search'] is known and the text-field will be filled with the search-string
            ?>
            <input placeholder="Straat" name="search" id="search" value="<?php echo $_GET['search']; ?>" />
            <?php
        } else { // If no seach has been given, $_GET['search'] will be unknown and the text-field will be left empty
            ?>
            <input placeholder="Straat" name="search" id="search" />
        <?php }
        ?>
        <input type="submit" name="Submit" value="Zoeken">
    </form>
    <br>
    Klik <a href="gebouwtoevoegen.php">hier</a> om een nieuw gebouw toe te voegen.
    <br>
    Klik <a href="gebouwdetailoverzicht.php">hier</a> om de mogelijke detail-velden van een gebouw te beheren.
    <br><br>

    <?php
    if (isset($_GET['start'])) { // If we are no longer on the first page, $_GET['start'] will be known and it's value will be saved in $start
        $start = $_GET['start'];
    } else { // If we are still on the first page, $_GET['start'] will be unknown and a value of 0 will be saved in $start
        $start = 0;
    }

    if (strlen($start) > 0 and !is_numeric($start)) { // A check on the value of $start
        exit;
    }
    $eu = ($start - 0);
    $limit = 20; // Number of records shown on each page
    $thi = $eu + $limit;
    $back = $eu - $limit;
    $next = $eu + $limit;
    if (isset($_GET['search'])) { // The SQL-commands when a search has been given, even when the search-string is empty
        $page_name = "gebouw.php?search=" . $_GET['search'] . "&";
        if ($_GET['search'] == "") {
            $nume = GetAllBuildingsCount();
        } else {

            $nume = GetAllBuildingsCountWithSearch($_GET['search']);
        }
    } else { // The SQL-command when no search has been given
        $page_name = "gebouw.php?";
        $nume = GetAllBuildingsCount();
    }
    ?>

    <table id="rounded-corner">
        <thead>
            <tr>
                <th scope="col" class="rounded-company">Adres</th>
                <th scope="col" class="rounded-q1">Postcode</th>
                <th scope="col" class="rounded-q2">Gemeente</th>
                <th scope="col" class="rounded-q3">Bouwjaar</th>
                <th scope="col" class="rounded-q3">Type</th>
                <th scope="col" class="rounded-q3">Details</th>
                <th scope="col" class="rounded-q4">Bewerken</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <td colspan="6" class="rounded-foot-left">
                    <?php
                    if ($limit < $nume) { // Creating the links for the different pages
                        if ($back >= 0) {
                            echo "<a href=" . $page_name . "start=" . $back . ">Vorige</a>";
                        }
                        $i = 0;
                        $l = 1;
                        for ($i = 0; $i < $nume; $i = $i + $limit) {
                            if ($i <> $eu) {
                                echo " <a href=" . $page_name . "start=" . $i . ">$l</a> ";
                            } else {
                                echo "<span style=\"font-weight:bold;\">" . $l . "</span>";
                            }
                            $l = $l + 1;
                        }
                        if ($thi < $nume) {
                            echo "<a href=" . $page_name . "start=" . $next . ">Volgende</a>";
                        }
                    }
                    ?>
                </td>
                <td class="rounded-foot-right">&nbsp;</td>
            </tr>
        </tfoot>
        <tbody>
            <?php
            // Getting all the records for a specific spage (e.g. first page, second page, ...)
            if (isset($_GET['search'])) {
                if ($_GET['search'] == "") {
                    $getAllBuildings = GetAllBuildingsLimit($eu, $limit);
                } else {
                    $getAllBuildings = GetAllBuildingsLimitWithSearch($eu, $limit, $_GET['search']);
                }
            } else {
                $getAllBuildings = GetAllBuildingsLimit($eu, $limit);
            }
            while ($building = mysql_fetch_assoc($getAllBuildings)) {

                echo "<tr><td>" . $building['Street'] . " " . $building['Number'] . "</td>";
                
                echo "<td>" . $building['ZIP'] . "</td>";
                echo "<td>" . $building['City'] . "</td>";

                $yearofconstruction = date("Y", strtotime($building['YearOfConstruction']));
                echo "<td>" . $yearofconstruction . "</td>";

                echo "<td>" . $building['Name'] . "</td>";
                
                echo "<td><a href=\"gebouwdetail.php?id=" . $building['Id'] . "\" >Bekijk details</a></td>";
                if (isset($_GET['start'])) {
                    if (isset($_GET['search'])) {
                        echo "<td><a href=\"gebouwaanpassen.php?id=" . $building['Id'] . "&search=" . $_GET['search'] . "&start=" . $_GET['start'] . "\" >Bewerken</a></td>";
                    } else {
                        echo "<td><a href=\"gebouwaanpassen.php?id=" . $building['Id'] . "&start=" . $_GET['start'] . "\" >Bewerken</a></td>";
                    }
                } else {
                    if (isset($_GET['search'])) {
                        echo "<td><a href=\"gebouwaanpassen.php?id=" . $building['Id'] . "&search=" . $_GET['search'] . "\" >Bewerken</a></td>";
                    } else {
                        echo "<td><a href=\"gebouwaanpassen.php?id=" . $building['Id'] . "\" >Bewerken</a></td>";
                    }
                }
            }
            ?>
        </tbody>
    </table>
</section>
<?php
include($_SERVER['DOCUMENT_ROOT'] . "/ObstacleWebApp/view/footer.php");
?>